/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.transfer;

import com.wtkj.vo.dzzf.base.kfwd.Account;
import com.wtkj.vo.dzzf.base.kfwd.AccountOpt;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue;
import com.wttech.dzzf.process.dao.DataTransferDao;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 数据迁移-业务账户检测
 *
 * @author MaShibo
 * @date 2017/11/6.
 */
@Service
public class AccountCheckService {

    private static final Logger ERRORINFO = DataTransferService.ERRORINFO;

    @Resource
    private DataTransferDao dataTransferDao;

    @Resource
    private AccountOptCheckService accountOptCheckService;

    @Resource
    private CardInfoCheckService cardInfoCheckService;

    /**
     * 分析业务账户信息
     *
     * @param account 业务账户信息
     */
    void analyseAccount(Account account) {

        checkAccountInfosIsValid(account);


        String accountId = account.getAccountId();
        Integer idNum = dataTransferDao.queryAccountId(accountId);
        if (idNum != null && idNum >= 1) {
            ERRORINFO.info("业务账户{}，ID【{}】已存在", accountId, accountId);
        }

        String accountNo = account.getAccountNo();
        Integer noNum = dataTransferDao.queryAccountNo(accountNo);
        if (noNum != null && noNum >= 1) {
            ERRORINFO.info("业务账户{}，编号【{}】已存在", accountId, accountNo);
        }

        //解析操作记录
        Optional<AccountOpt> opt = dataTransferDao.queryAccountOpt(accountId);
        if (opt.isPresent()) {
            accountOptCheckService.analyseAccountOpt(opt.get());
        } else {
            ERRORINFO.info("业务账户{}, 开户操作记录不存在", accountId);
        }

        //解析卡片信息
        List<CardInfo> cardInfos = dataTransferDao.queryCardInfosByAccountId(accountId);
        if (CollectionUtils.isEmpty(cardInfos)) {
            ERRORINFO.info("业务账户{}，不存在对应的卡片信息", accountId);
        } else {
            cardInfos.forEach(cardInfo -> cardInfoCheckService.analyseCardInfo(cardInfo));
        }

    }

    /**
     * 检查业务账户信息是否错误
     *
     * @param account 业务账户信息
     */
    private void checkAccountInfosIsValid(Account account) {

        String accountId = account.getAccountId();
        //账户类型
        Integer accountType = account.getAccountType();
        if (accountType == null || (!accountType.equals(CardStaticValue.EcardType.CARD_TYPE_CZ) &&
                !accountType.equals(CardStaticValue.EcardType.CARD_TYPE_JZ))) {
            ERRORINFO.info("业务账户{}，账户类型【{}】错误", accountId, accountType);
        }

        //TOPCLASS
        String topClass = account.getTopClass();
        if (!StringUtils.equalsIgnoreCase(FundAccountCheckService.DEFAULT_TOPCLASS, topClass)) {
            ERRORINFO.info("业务账户{}，TOPCLASS【{}】错误", accountId, topClass);
        }

    }
}
